home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / Macintosh Tracker 1.1 Source / Tracker Server Folder / read.c < prev    next >
Text File  |  1993-04-25  |  10KB  |  419 lines

  1. /* read.c */
  2.  
  3. /* $Id: read.c,v 3.5 1992/11/27 10:29:00 espie Exp espie $
  4.  * $Log: read.c,v $
  5.  * Revision 3.5  1992/11/27  10:29:00  espie
  6.  * General cleanup
  7.  *
  8.  * Revision 3.4  1992/11/23  17:18:59  espie
  9.  * *** empty log message ***
  10.  *
  11.  * Revision 3.3  1992/11/23  10:12:23  espie
  12.  * *** empty log message ***
  13.  *
  14.  * Revision 3.2  1992/11/22  17:20:01  espie
  15.  * Checks for finetune ?
  16.  *
  17.  * Revision 3.1  1992/11/19  20:44:47  espie
  18.  * Protracker commands.
  19.  *
  20.  * Revision 3.0  1992/11/18  16:08:05  espie
  21.  * New release.
  22.  *
  23.  * Revision 2.16  1992/11/17  17:06:25  espie
  24.  * fix_xxx for better speed.
  25.  * Added some sample information in the dump.
  26.  * Added transpose feature.
  27.  * Feature fix: length 1 sample should be empty.
  28.  * Corrected repeat length problems concerning badly formed files,
  29.  * added signature checking for new tracker files.
  30.  * Corrected small problem with repeat being too short.
  31.  * Coded error types. More amiga specific stuff.
  32.  *
  33.  * Revision 1.17  1991/11/17  16:30:48  espie
  34.  * Rationnalized error recovery.
  35.  * There was a bug: you could try to deallocate
  36.  * stuff in no-noland. Also, strings never got
  37.  * to be freed.
  38.  * Centralized error control to error_song.
  39.  * Added a new test on length, aborts most modules now.
  40.  * Maybe should say it as well.
  41.  * Added checkpoints for early return if file too short.
  42.  * Added memory recovery and error control.
  43.  * Suppressed ! warning for bad note.
  44.  * Added note support.
  45.  * Corrected length and rep_length/rep_offset
  46.  * which are given in words and should be converted to
  47.  * bytes.
  48.  */
  49.  
  50. #include <malloc.h>
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <ctype.h>
  54. #include <assert.h>
  55.  
  56. #include "defs.h"
  57. #include "extern.h"
  58. #include "song.h"
  59. #include "channel.h"
  60.  
  61. LOCAL char *id = "$Id: read.c,v 3.5 1992/11/27 10:29:00 espie Exp espie $";
  62.  
  63. /***
  64.  *
  65.  *    Low level st-file access routines 
  66.  *
  67.  ***/
  68.  
  69. /* c = checkgetc(f):
  70.  * gets a character from file f.
  71.  * Aborts program if file f is finished
  72.  */
  73. LOCAL int checkgetc(f)
  74. FILE *f;
  75.     {
  76.     int c;
  77.  
  78.     if ((c = fgetc(f)) == EOF)
  79.         error = FILE_TOO_SHORT;
  80.     return c;
  81.     }
  82.  
  83. #define MAX_LEN 50
  84. /* s = getstring(f, len):
  85.  * gets a soundtracker string from file f.
  86.  * I.e, it is a fixed length string terminated
  87.  * by a 0 if too short. Length should be
  88.  * smaller than MAX_LEN.
  89.  */
  90. LOCAL char *getstring(f, len)
  91. FILE *f;
  92. int len;
  93.     {
  94.     static char s[MAX_LEN];
  95.     char *new;
  96.     int i;
  97.         
  98.     assert(len < MAX_LEN);
  99.     for (i = 0; i < len; i++)
  100.         s[MIN(i, MAX_LEN - 1)] = checkgetc(f);
  101.     s[MIN(len, MAX_LEN - 1)] = '\0';
  102.     new = malloc(strlen(s)+1);
  103.  
  104.     return strcpy(new, s);
  105.     }
  106.  
  107. /* byteskip(f, len)
  108.  * same as fseek, xcpt it works on stdin
  109.  */
  110. LOCAL void byteskip(f, len)
  111. FILE *f;
  112. int len;
  113.     {
  114.     int i;
  115.  
  116.     for (i = 0; i < len; i++)
  117.         checkgetc(f);
  118.     }
  119.  
  120. /* v = getushort(f)
  121.  * reads an unsigned short from f
  122.  */
  123. LOCAL int getushort(f)
  124. FILE *f;
  125.     {
  126.     int i;
  127.  
  128.     i = checkgetc(f) << 8;
  129.     return i | checkgetc(f);
  130.     }
  131.  
  132.  
  133. /* fill_sample_info(info, f):
  134.      fill sample info with the information at current position of
  135.      file f. Allocate memory for storing the sample, also.
  136.     fill_sample_info is guaranteed to give you an accurate snapshot
  137.     of what sample should be like. In particular, length, rp_length,
  138.     start, rp_start, fix_length, fix_rp_length will have the values
  139.     you can expect if part of the sample is missing.
  140.  */
  141. LOCAL void fill_sample_info(info, f)
  142. struct sample_info *info;
  143. FILE *f;
  144.     {
  145.     info->name = getstring(f, SAMPLENAME_MAXLENGTH);
  146.     info->length = getushort(f);
  147.     info->finetune = checkgetc(f);
  148.     if (info->finetune > 15)
  149.         info->finetune = 0;
  150.     info->volume = checkgetc(f);
  151.     info->volume = MIN(info->volume, MAX_VOLUME);
  152.     info->rp_offset = getushort(f);
  153.     info->rp_length = getushort(f);
  154.  
  155.     /* the next check is for old modules for which
  156.      * the sample data types are a bit confused, so
  157.      * that what we were expecting to be #words is #bytes.
  158.      */
  159.         /* not sure I understand the -1 myself, though it's
  160.          * necessary to play kawai-k1 correctly 
  161.          */
  162.     if (info->rp_length + info->rp_offset - 1 > info->length)
  163.         info->rp_offset /= 2;
  164.     
  165.     if (info->rp_length + info->rp_offset > info->length)
  166.         info->rp_length = info->length - info->rp_offset;
  167.  
  168.     info->length *= 2;
  169.     info->rp_offset *= 2;
  170.     info->rp_length *= 2;
  171.         /* in all logic, a 2-sized sample could exist,
  172.          * but this is not the case, and even so, some
  173.          * trackers output empty instruments as being 2-sized.
  174.          */
  175.     if (info->length <= 2)
  176.         {
  177.         info->start = NULL;
  178.         info->length = 0;
  179.         }
  180.     else
  181.         {
  182.         info->start = (SAMPLE *)calloc(info->length, 1);
  183.  
  184.         if (info->rp_length > 2)
  185.             info->rp_start = info->start + info->rp_offset;
  186.         else
  187.             {
  188.             info->rp_start = NULL;
  189.             info->rp_length = 0;
  190.             }
  191.         }
  192.  
  193.     if (info->length > MAX_SAMPLE_LENGTH)
  194.         error = CORRUPT_FILE;
  195.     info->fix_length = int_to_fix(info->length);
  196.     info->fix_rp_length = int_to_fix(info->rp_length);
  197.     }
  198.  
  199. LOCAL void fill_song_info(info, f)
  200. struct song_info *info;
  201. FILE *f;
  202.     {
  203.     int i;
  204.     int p;
  205.  
  206.     info->length = checkgetc(f);
  207.     checkgetc(f);
  208.     info->maxpat = -1;
  209.     for (i = 0; i < NUMBER_PATTERNS; i++)
  210.         {
  211.         p = checkgetc(f);
  212.         if (p >= NUMBER_PATTERNS)
  213.             p = 0;
  214.         if (p > info->maxpat)
  215.             info->maxpat = p;
  216.         info->patnumber[i] = p;
  217.         }
  218.     info->maxpat++;
  219.     if (info->maxpat == 0 || info->length == 0)
  220.         error = CORRUPT_FILE;
  221.     }
  222.  
  223. LOCAL void fill_event(e, f)
  224. struct event *e;
  225. FILE *f;
  226.     {
  227.     int a, b, c, d;
  228.  
  229.     a = checkgetc(f);
  230.     b = checkgetc(f);
  231.     c = checkgetc(f);
  232.     d = checkgetc(f);
  233.     e->sample_number = (a & 0x10) | (c >> 4);
  234.     e->effect = c & 0xf;
  235.     e->parameters = d;
  236.     e->pitch = ( (a & 15) << 8 ) | b;
  237.     e->note = find_note(e->pitch);
  238.     }
  239.  
  240. LOCAL void fill_pattern(pattern, f)
  241. struct block *pattern;
  242. FILE *f;
  243.     {
  244.     int i, j;
  245.  
  246.     for (i = 0; i < BLOCK_LENGTH; i++)
  247.         for (j = 0; j < NUMBER_TRACKS; j++)
  248.             fill_event(&(pattern->e[j][i]), f);
  249.     }
  250.  
  251.  
  252. LOCAL void read_sample(struct sample_info *info, FILE *f)
  253.     {
  254.  
  255.     if (info->start)
  256.         {
  257.         fread(info->start, 1, info->length, f);
  258. /*@ fixed this for anti-aliasing */
  259.         if (info->rp_start)
  260.           {
  261.             /* if sample loops, then append first loop sample to end */
  262.             info->start[info->length] = info->start[info->rp_offset];
  263.           }
  264.          else
  265.           {
  266.             /* otherwise, append last sample */
  267.             info->start[info->length] = info->start[info->length - 1];
  268.           }
  269.         }
  270.     }
  271.  
  272.  
  273.  
  274.  
  275. /***
  276.  *
  277.  *  new_song: allocates a new structure for a song.
  278.  *  clears each and every field as appropriate.
  279.  *
  280.  ***/
  281. LOCAL struct song *new_song()
  282.     {
  283.     struct song *new;
  284.     int i;
  285.  
  286.     new = (struct song *)malloc(sizeof(struct song));
  287.     new->title = NULL;
  288.     new->info.length = 0;
  289.     new->info.maxpat = -1;
  290.     new->info.transpose = 0;
  291.     new->info.pblocks = NULL;
  292.     for (i = 0; i < NUMBER_SAMPLES; i++)
  293.         {
  294.         new->samples[i].finetune = 0;
  295.         new->samples[i].name = NULL;
  296.         new->samples[i].length = NULL;
  297.         new->samples[i].start = NULL;
  298.         new->samples[i].rp_start = NULL;
  299.         }
  300.     return new;
  301.     }
  302.  
  303. /* release_song(song): gives back all memory 
  304.  * occupied by song. Assume that each structure
  305.  * has been correctly allocated by a call to the
  306.  * corresponding new_XXX function.
  307.  */
  308. void release_song(song)
  309. struct song *song;
  310.     {
  311.     int i;
  312.  
  313.     for (i = 0; i < NUMBER_SAMPLES; i++)
  314.         {
  315.         if (song->samples[i].start)
  316.             free(song->samples[i].start);
  317.         if (song->samples[i].name)
  318.             free(song->samples[i].name);
  319.         }
  320.     if (song->info.pblocks)
  321.         free(song->info.pblocks);
  322.     if (song->title)
  323.         free(song->title);
  324.     free(song);
  325.     }
  326.  
  327. /* error_song(song): what we should return
  328.  * if there was an error. Actually, is mostly
  329.  * useful for its side effects.
  330.  */
  331. LOCAL struct song *error_song(song)
  332. struct song *song;
  333.     {
  334.     release_song(song);
  335.     return NULL;
  336.     }
  337.  
  338. /* bad_sig(f): read the signature on file f
  339.  * and returns TRUE if it is not a known sig.
  340.  */
  341. LOCAL BOOL bad_sig(f)
  342. FILE *f;
  343.     {
  344.     char a, b, c, d;
  345.  
  346.     a = checkgetc(f);
  347.     b = checkgetc(f);
  348.     c = checkgetc(f);
  349.     d = checkgetc(f);
  350.     if (a == 'M' && b == '.' && c == 'K' && d == '.')
  351.         return FALSE;
  352.     if (a == 'M' && b == '&' && c == 'K' && d == '!')
  353.         return FALSE;
  354.     if (a == 'F' && b == 'L' && c == 'T' && d == '4')
  355.         return FALSE;
  356.     return TRUE;
  357.     }
  358.  
  359. /* s = read_song(f, type): tries to read a song s
  360.  * of type NEW/OLD in file f. Might fail, i.e.,
  361.  * returns NULL if file is not a mod file of the
  362.  * correct type.
  363.  */
  364. struct song *read_song(f, type)
  365. FILE *f;
  366. int type;
  367.     {
  368.     struct song *song;
  369.     int i;
  370.     int ninstr;
  371.  
  372.     error = NONE;
  373.     if (type == NEW || type == NEW_NO_CHECK)
  374.         ninstr = 31;
  375.     else
  376.         ninstr = 15;
  377.  
  378.     song = new_song();
  379.     song->title = getstring(f, TITLE_MAXLENGTH);
  380.     if (error != NONE)
  381.         return error_song(song);
  382.  
  383.     for (i = 1; i <= ninstr; i++)
  384.         {
  385.         fill_sample_info(&song->samples[i], f);
  386.         if (error != NONE)
  387.             return error_song(song);
  388.         }
  389.  
  390.     fill_song_info(&song->info, f);
  391.  
  392.     if (error != NONE)
  393.         return error_song(song);
  394.  
  395.     if (type == NEW && bad_sig(f))
  396.         return error_song(song);
  397.  
  398.     if (type == NEW_NO_CHECK)
  399.         byteskip(f, 4);
  400.         
  401.  
  402.     song->info.pblocks = (struct block *)
  403.         malloc(sizeof(struct block) * song->info.maxpat);
  404.     for (i = 0; i < song->info.maxpat; i++)
  405.         {
  406.         fill_pattern(song->info.pblocks + i, f);
  407.         if (error != NONE)
  408.             return error_song(song);
  409.         }
  410.  
  411.     for (i = 1; i <= ninstr; i++)
  412.         read_sample(&song->samples[i], f);
  413.     
  414.     if (error != NONE)
  415.         return error_song(song);
  416.     return song;
  417.     }
  418.  
  419.